import Api from '../../constants/Api'

export default {
  namespaced: true,
  state: {
    enumTypeList: [],
    enumDown: [],
    contractType: []
  },
  mutations: {
    enumListTo(state, data) {
      state.enumTypeList = data
    },
    enumDownTo(state, data) {
      state.enumDown = data
    },
    contractTypeListTo(state, data) {
      state.contractType = data
    }
  },
  actions: {
    getEnumList({
      commit
    }, that) {
      return that.$axios({
        method: 'get',
        url: Api.getEnumList
      }).then(res => {
        return res.data
      })
    },
    // getEnumDown({
    //   commit
    // }, that) {
    //   that.$axios({
    //     method: 'get',
    //     url: Api.getEnumList,
    //     params: {
    //       enumType: that.enumType,
    //       parentId: that.parentId ? that.parentId : ''
    //     }
    //   }).then(res => {
    //     commit('enumDownTo', res.data)
    //   }).catch(() => {})
    // },
    // getEnumDown({
    //   commit
    // }, that) {
    //   return new Promise((resolve, reject) => {
    //     that.$axios({
    //       method: 'get',
    //       url: Api.getEnumList,
    //       params: {
    //         enumType: that.enumType,
    //         parentId: that.parentId ? that.parentId : ''
    //       }
    //     }).then(res => {
    //       return resolve(res.data)
    //     }).catch(() => {
    //       return reject(err.data.msg)
    //     })
    //   })
    // },
    getEnumDown({commit}, that) {
      return that.$axios({
        method: 'get',
        url: Api.getEnumList,
        params: {
          enumType: that.enumType,
          parentId: that.parentId ? that.parentId : ''
        }
      }).then(res => {
        return res.data
      }).catch(err => {
        that.$message.error(err.data.msg)
      })
    },
    getEnumByType({
      commit
    }, that) {
      that.$axios({
        method: 'get',
        url: Api.getEnumList,
        params: {
          current: that.current,
          pageSize: that.pageSize,
          enumType: that.enumType,
        }
      }).then(res => {
        commit('enumListTo', res.data.records)
        that.total = res.data.total
        that.tableLoading = false
      }).catch(() => {
        that.tableLoading = false
      })
    },
    addEnum({
      commit
    }, that) {
      that.$axios({
        method: 'post',
        url: Api.addEnum,
        data: that.enumForm
      }).then(res => {
        if (res.code === 20002) {
          that.handleVisible = false
          that.$message.success(res.msg)
          that.current = 1
          that.getEnumType()
        } else {
          that.$message.error(res.msg)
        }
      }).catch(error => {
        that.$message.error(error.data.message)
      })
    },
    editEnum({
      commit
    }, that) {
      that.$axios({
        method: 'post',
        url: Api.editEnum,
        data: that.enumForm
      }).then(res => {
        if (res.code === 20003) {
          that.handleVisible = false
          that.$message.success(res.msg)
          that.getEnumType()
        } else {
          that.$message.error(res.msg)
        }
      }).catch(error => {
        that.$message.error(error.data.message)
      })
    },
    delEnum({
      state
    }, that) {
      let url = Api.delEnum.replace('{enumId}', that.enumId)
      return that.$axios({
        method: 'delete',
        url: url
      }).then(res => {
        if (res.code === 20004) {
          that.delVisible = false
          that.getEnumType()
          that.$message.success(res.msg)
        } else {
          that.$message.error(res.msg)
        }
      }).catch(error => {
        that.$message.error(error.data.message)
      })
    }
  }
}